package com.itheima.reggie.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Override
    public void updatemoren(Long id) {
//        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Address::getUserId, UserHolder.get().getId());
//        List<Address> addressList = addressService.list(wrapper);
//        if (CollectionUtil.isNotEmpty(addressList)) {
//            for (Address address : addressList) {
//                address.setIsDefault(0);
//                addressService.updateById(address);
//            }
//        }
        LambdaUpdateWrapper<Address> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Address::getUserId, UserHolder.get().getId());

        Address address1 = new Address();
        address1.setIsDefault(0);
        addressMapper.update(address1,wrapper);

        Address address = addressMapper.selectById(id);
        address.setIsDefault(1);
        addressMapper.updateById(address);
    }
}
